<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        let obj = {
            name: "hah",
            age: 18,
            arr: [1, 2, 3],
            ob: {
                name: "test",
                age: 18,
                firends: ["test1", "test2"]
            }
        };
        let newObj = {};
        for (let key in obj) {
            newObj[key] = obj[key];
            //newObj.name = obj.name
            //newObj.age = obj.age
            //newObj.arr = obj.arr
            //newObj.ob = obj.ob
        }
        console.log(newObj);

        const obj1 = {
            age: 20,
            name: 'xxx',
            address: {
                city: 'beijing'
            },
            arr: ['a', 'b', 'c'],
            name2: null
        };
        function deepClone(obj = {}) {
            if (typeof obj !== 'object' || obj == null) {
                return obj
            }
            let result;
            if (obj instanceof Array) {
                result = []
            } else {
                result = {}
            }
            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    result[key] = deepClone(obj[key])
                }
            }
            return result
        }

        function deepClone(obj) {
            if (typeof obj !== 'object' || obj == null) {
                return obj
            }
            let result;
            if (obj instanceof Array) {
                result = []
            } else {
                result = {}
            }

            for (let key in obj) {
                result[key] = deepClone(obj[key]);
            }
            return result
        }
        const obj1 = {
            age: 20,
            name: 'xxx',
            address: {
                city: 'beijing',
                address: {
                    city: 'beijing'
                },
            },
            arr: ['a', 'b', 'c']
        };

        let newObj = {};
        for (let key in obj) {
            if (typeof obj1[key] !== "object") {
                newObj[key] = obj1[key];
            } else {
                newObj[key] = clone(obj1[key])
            }
        }

        function clone(obj) {
            if (typeof obj !== "object") {
                return obj;
            }
            let newObj = {};
            if (obj instanceof Array) {
                newObj = []
            }
            for (let key in obj) {
                newObj[key] = clone(obj[key]);
            }
            return newObj
        }
    </script>
</body>

</html>